{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f456892f",
   "metadata": {},
   "source": [
    "* 周次：week09\n",
    "* 时间：2022-10-27\n",
    "* 学习记录人：yy\n",
    "* 学习主题：用户输入-文本的格式化"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5e348ef6",
   "metadata": {},
   "source": [
    "# 本周知识点\n",
    "> 1.用户输入  \n",
    "> 2.文本的格式化输出  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aea9b9f1",
   "metadata": {},
   "source": [
    "## 1.用户输入"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f45f6609",
   "metadata": {},
   "source": [
    "* input方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "051ba308",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 用户登录系统\n",
    "username = input(\"账号：\")\n",
    "password = input(\"密码：\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d6aaa2d3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "admin\n",
      "123456\n"
     ]
    }
   ],
   "source": [
    "print(username)\n",
    "print(password)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "514f996b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "账号：1223\n",
      "密码：3343\n",
      "账号不存在或者密码不正常！请重新输入！\n"
     ]
    }
   ],
   "source": [
    "# 用户登录系统 迭代1\n",
    "user01 = \"admin\"\n",
    "password01 = \"123456\"\n",
    "\n",
    "username = input(\"账号：\")\n",
    "password = input(\"密码：\")\n",
    "#情景1：账号密码都正确\n",
    "if user01 == username and password01 == password:\n",
    "    print(\"登录成功 ~欢迎\"+user01)\n",
    "\n",
    "         \n",
    "#情景2：账号不存在或密码不正确\n",
    "else:\n",
    "   print(\"账号不存在或者密码不正常！请重新输入！\") \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "f3a5e468",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "账号：admin\n",
      "密码：123456\n",
      "登录成功 ~欢迎admin\n"
     ]
    }
   ],
   "source": [
    "# 用户登录系统 迭代2 循环！\n",
    "\n",
    "user01 = \"admin\"\n",
    "password01 = \"123456\"\n",
    "\n",
    "for i in range(3,0,-1):\n",
    "    username = input(\"账号：\")\n",
    "    password = input(\"密码：\")\n",
    "    #情景1：账号密码都正确\n",
    "    if user01 == username and password01 == password:\n",
    "        print(\"登录成功 ~欢迎\"+user01)\n",
    "        break # 中止循环\n",
    "    #情景2：账号不存在或密码不正确，用户可以输入三次，三次之后1个小时之后再来尝试\n",
    "    else:\n",
    "        if i >1:\n",
    "             print(\"账号不存在或者密码不正确！请重新输入！您还有\"+str(i-1)+\"次尝试的机会\") \n",
    "        else:\n",
    "            print(\"账号不存在，请1个小时后在尝试\")\n",
    "        "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c3f220af",
   "metadata": {},
   "source": [
    "## 2.字符串的格式化输出\n",
    "\n",
    "> 1.字符串的拼接  \n",
    "> 2.占位符  \n",
    "> 3.格式化输出的方法：format()  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c9e6d667",
   "metadata": {},
   "source": [
    "* 字符串的拼接"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "1af44bbc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "昵称：小哈\n",
      "地区：广州\n",
      "工作：产品经理助理\n",
      "年龄：22\n",
      "期待薪资：30k\n"
     ]
    }
   ],
   "source": [
    "# 用户的信息输入（完善信息）:字符串的拼接\n",
    "name = input(\"昵称：\")\n",
    "region= input(\"地区：\")\n",
    "job = input(\"工作：\")\n",
    "age = input(\"年龄：\")\n",
    "salary = input(\"期待薪资：\")\n",
    "\n",
    "info = '''-------------INFO'''+ name + '''------------\n",
    "地区：''' + region + '''\n",
    "工作：''' + job + '''\n",
    "年龄：''' + age + '''\n",
    "薪资期待：''' + salary "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "3ec6efd4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-------------INFO小哈------------\n",
      "地区：广州\n",
      "工作：产品经理助理\n",
      "年龄：22\n",
      "薪资期待：30k\n"
     ]
    }
   ],
   "source": [
    "print(info)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b8a87148",
   "metadata": {},
   "source": [
    "* 占位符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "8ef7fe45",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "昵称：gfgf\n",
      "地区：sdsd\n",
      "工作：dfgf\n",
      "年龄：sadsd\n",
      "期待薪资：fdfdf\n",
      "-------------INFO gfgf------------\n",
      "地区：sdsd\n",
      "工作：dfgf\n",
      "年龄：sadsd\n",
      "薪资期待：fdfdf \n"
     ]
    }
   ],
   "source": [
    "# 用户的信息输入（完善信息）\n",
    "name = input(\"昵称：\")\n",
    "region= input(\"地区：\")\n",
    "job = input(\"工作：\")\n",
    "age = input(\"年龄：\")\n",
    "salary = input(\"期待薪资：\")\n",
    "\n",
    "info = '''-------------INFO %s------------\n",
    "地区：%s\n",
    "工作：%s\n",
    "年龄：%s\n",
    "薪资期待：%s ''' %(name,region,job,age,salary)\n",
    "print(info)\n",
    "# 局限性：有序，个数确定，不能乱写"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4d74ecd2",
   "metadata": {},
   "source": [
    "* 格式化输出的方法：format()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "id": "7c1bca77",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "昵称：poo\n",
      "地区：ijj\n",
      "工作：\n",
      "年龄：19\n",
      "期待薪资：8k\n",
      "-------------INFO poo ------------\n",
      "地区：ijj\n",
      "工作：\n",
      "年龄：19\n",
      "薪资期待：8k \n"
     ]
    }
   ],
   "source": [
    "# 用户的信息输入（完善信息）：format()\n",
    "name = input(\"昵称：\")\n",
    "region= input(\"地区：\")\n",
    "job = input(\"工作：\")\n",
    "age = input(\"年龄：\")\n",
    "salary = input(\"期待薪资：\")\n",
    "\n",
    "info = '''-------------INFO {昵称} ------------\n",
    "地区：{地区}\n",
    "工作：{工作}\n",
    "年龄：{年龄}\n",
    "薪资期待：{期待薪资} '''. format(昵称=name,地区=region,工作=job,年龄=age,期待薪资=salary)\n",
    "print(info)\n",
    "# 书写规范，字符串中的字段一一对应，不考虑其顺序"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a8e53947",
   "metadata": {},
   "source": [
    "### 课后作业（第二章）\n",
    "> 1.第二章P93 前的所有项目练习(下周讲）  \n",
    "> 2.购物车项目  \n",
    ">> 要求：  \n",
    ">> 1.运行程序后，让用户输入支付宝余额，然后打印我们的商品列表给用户。  \n",
    ">> 2.让用户输入商品编号进行商品的购买。  \n",
    ">> 3.用户选择商品后，检查用户的余额是否够，若够则直接扣款，不够则提醒用户。  \n",
    ">> 4.用户可以随时退出购买，推出时打印用户已购买的商品和支付宝余额。  \n",
    "\n",
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "85739169",
   "metadata": {},
   "source": [
    "* 1.第二章P93 前的所有项目练习"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "78fa6aee",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Don't panic!\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "['o', 'n', ' ', 't', 'a', 'p']\n",
      "on tap\n"
     ]
    }
   ],
   "source": [
    "phrase = \"Don't panic!\"\n",
    "plist = list(phrase)\n",
    " print(phrase) \n",
    "    print(plist)\n",
    "for i in range(4):\n",
    "    plist.pop()\n",
    "plist.pop(0)\n",
    "plist.remove(\"'\")\n",
    "plist.extend([plist.pop(), plist.pop()])\n",
    "plist.insert(2, plist.pop(3))\n",
    "new_phrase = ''.join(plist)\n",
    " print(plist)\n",
    "    print(new_phrase)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "833d8b96",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "i\n",
      "i\n",
      "a\n"
     ]
    }
   ],
   "source": [
    "vowels = ['a', 'e', 'i', 'o', 'u']\n",
    "word = \"Milliways\"\n",
    "for letter in word:\n",
    "    if letter in vowels:\n",
    "        print(letter)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "fa384ff1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "i\n",
      "a\n"
     ]
    }
   ],
   "source": [
    "vowels = ['a', 'e', 'i', 'o', 'u']\n",
    "word = \"Milliways\"\n",
    "found = []\n",
    "for letter in word:\n",
    "    if letter in vowels:\n",
    "        if letter not in found:\n",
    "            found.append(letter)\n",
    "for vowel in found:\n",
    "    print(vowel)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "id": "471188a9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search for vowels:. 1223\n"
     ]
    }
   ],
   "source": [
    "vowels = ['a', 'e', 'i', 'o', 'u']\n",
    "word = input(\"Provide a word to search for vowels:. \")\n",
    "found = []\n",
    "for letter in word:\n",
    "    if letter in vowels:\n",
    "        if letter not in found:\n",
    "            found.append(letter)\n",
    "for vowel in found:\n",
    "    print(vowel)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "7706de1e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Don't panic!\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "['o', 'n', ' ', 't', 'a', 'p']\n",
      "on tap\n"
     ]
    }
   ],
   "source": [
    "phrase = \"Don't panic!\"\n",
    "plist = list(phrase)\n",
    "print(phrase) \n",
    "print(plist)\n",
    "\n",
    "for i in range(4):\n",
    "    plist.pop()\n",
    "plist.pop(0)\n",
    "plist.remove(\"'\")\n",
    "plist.extend([plist.pop(), plist.pop()])\n",
    "plist.insert(2, plist.pop(3))\n",
    "\n",
    "new_phrase = ''.join(plist)\n",
    "print(plist)\n",
    "print(new_phrase)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "47d1027f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Don't panic!\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "on tap\n"
     ]
    }
   ],
   "source": [
    "phrase = \"Don't panic!\"\n",
    "plist = list(phrase)\n",
    "print(phrase) \n",
    "print(plist)\n",
    "\n",
    "new_phrase = ''.join(plist[1:3])\n",
    "new_phrase = new_phrase + ''.join([plist[5], plist[4], plist[7], plist[6]])\n",
    "\n",
    "print(plist)\n",
    "print(new_phrase)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "eae45a38",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\t M\n",
      "\t a\n",
      "\t r\n",
      "\t v\n",
      "\t i\n",
      "\t n\n"
     ]
    }
   ],
   "source": [
    "paranoid_android = \"Marvin\"\n",
    "letters = list(paranoid_android)\n",
    "for char in letters:\n",
    "    print('\\t', char)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "1a0b970f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\t M\n",
      "\t a\n",
      "\t r\n",
      "\t v\n",
      "\t i\n",
      "\t n\n",
      "\n",
      "\t\t A\n",
      "\t\t n\n",
      "\t\t d\n",
      "\t\t r\n",
      "\t\t o\n",
      "\t\t i\n",
      "\t\t d\n",
      "\n",
      "\t\t\t P\n",
      "\t\t\t a\n",
      "\t\t\t r\n",
      "\t\t\t a\n",
      "\t\t\t n\n",
      "\t\t\t o\n",
      "\t\t\t i\n",
      "\t\t\t d\n"
     ]
    }
   ],
   "source": [
    "paranoid_android = \"Marvin, the Paranoid Android\"\n",
    "letters = list(paranoid_android)\n",
    "for char in letters[:6]:\n",
    "    print('\\t', char)\n",
    "print()\n",
    "for char in letters[-7:]:\n",
    "    print('\\t'*2, char)\n",
    "print()\n",
    "for char in letters[12:20]:\n",
    "    print('\\t'*3, char)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "348485a4",
   "metadata": {},
   "source": [
    "* 购物车项目\n",
    ">> 要求：  \n",
    ">> 1.运行程序后，让用户输入支付宝余额，然后打印我们的商品列表给用户。  \n",
    ">> 2.让用户输入商品编号进行商品的购买。  \n",
    ">> 3.用户选择商品后，检查用户的余额是否够，若够则直接扣款，不够则提醒用户。  \n",
    ">> 4.用户可以随时退出购买，推出时打印用户已购买的商品和支付宝余额。 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "6e36943f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入您的账号:123\n",
      "请输入您的密码:456\n",
      "恭喜，123,登陆成功，欢迎您\n",
      "请输入您的余额：1000000\n",
      "\n",
      "        ------------------------------------\n",
      "                  欢迎光临购物商城         \n",
      "        ------------------------------------            \n",
      "        \n",
      "\n",
      "        1:获取商品列表\n",
      "        2:充值购物卡\n",
      "        \n",
      "请选择你的功能1\n",
      "序号：1 商品：面包 价格：20元\n",
      "序号：2 商品：猪肉 价格：30元\n",
      "序号：3 商品：紫米 价格：15元\n",
      "序号：4 商品：可乐 价格：3.5元\n",
      "请输入你要购买的商品，输入[0]退出0\n",
      "退出程序，再见\n"
     ]
    }
   ],
   "source": [
    "username01 = \"123\"\n",
    "password01 = \"456\"\n",
    "list_he=[] \n",
    "\n",
    "for i in range(3):\n",
    "    username = input(\"请输入您的账号:\")\n",
    "    password = input(\"请输入您的密码:\")\n",
    "    if username == username01 and password == password01: \n",
    "        print(\"恭喜，%s,登陆成功，欢迎您\" % username)\n",
    "        offer = int(input(\"请输入您的余额：\"))\n",
    "        print('''\n",
    "        ------------------------------------\n",
    "                  欢迎光临购物商城         \n",
    "        ------------------------------------            \n",
    "        ''')\n",
    "        \n",
    "        print('''\n",
    "        1:获取商品列表\n",
    "        2:充值购物卡\n",
    "        ''')\n",
    "        \n",
    "        功能 = int(input(\"请选择你的功能\"))\n",
    "        if 功能 == 1:  \n",
    "            while True:\n",
    "                shipin2 = [['面包', 20], ['猪肉', 30], ['紫米', 15], ['可乐', 3.5]]\n",
    "                for i, a in enumerate(shipin2, 1):  \n",
    "                    print(\"序号：%s\" % i, \"商品：%s\" % a[0], \"价格：%s元\" % a[1])\n",
    "                huoqu = int(input(\"请输入你要购买的商品，输入[0]退出\"))\n",
    "                if huoqu > 0 and huoqu <= len(shipin2):    \n",
    "                        j=shipin2[huoqu-1] \n",
    "                        if j[1]>offer :        \n",
    "                            print(\"您的余额不足,请及时充值\")\n",
    "                        else:\n",
    "                            offer = offer - j[1]   \n",
    "                            print(\"您购买的商品为%s\" % j[0], \"剩余金额为%s\" % offer)    \n",
    "                            list_he.append(j[0])     \n",
    "                            print(\"您已经购买了%s\"%list_he)       \n",
    "                elif huoqu==0:\n",
    "                    print(\"退出程序，再见\")\n",
    "                    for m in list_he :\n",
    "                        print(\"您购买了%s\"%m)\n",
    "                    break\n",
    "                else:\n",
    "                    print(\"商城货物暂时短缺，请输入正确的商品序号\")\n",
    "        elif 功能 == 2:\n",
    "            chongzhi=int(input(\"请输入您要充值的金额\"))\n",
    "            if chongzhi>0:\n",
    "                offer+=chongzhi\n",
    "                print(\"充值成功,现余额为%s\"%offer) \n",
    "            else:\n",
    "                print(\"充值失败\")\n",
    "                break\n",
    "        elif 功能 != 1 and 2: \n",
    "            print(\"退出系统成功\")\n",
    "        break\n",
    "    else:\n",
    "        print(\"账号或密码输入不正确,请重新输入\")\n",
    "\n",
    "else:\n",
    "    print(\"尝试用户名或密码超过最大次数，请重新运行程序\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4c870f34",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
